// 通用校验规则
export const required = (message = '该字段是必填项') => ({
  required: true,
  message,
  trigger: 'blur'
})

// 字符串长度校验
export const lengthRange = (min, max, message) => ({
  min,
  max,
  message: message || `长度应在 ${min} 到 ${max} 个字符之间`,
  trigger: 'blur'
})

// 数字范围校验
export const numberRange = (min, max, message) => ({
  type: 'number',
  min,
  max,
  message: message || `数值应在 ${min} 到 ${max} 之间`,
  trigger: 'blur'
})

// 邮箱校验
export const email = (message = '请输入有效的邮箱地址') => ({
  type: 'email',
  message,
  trigger: ['blur', 'change']
})

// 手机号码校验
export const phone = (message = '请输入有效的手机号码') => ({
  pattern: /^1[3-9]\d{9}$/,
  message,
  trigger: 'blur'
})

// 车牌号校验
export const plateNumber = (message = '请输入有效的车牌号') => ({
  pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-Z0-9]{5,6}$/,
  message,
  trigger: 'blur'
})

// 身份证号校验
export const idCard = (message = '请输入有效的身份证号') => ({
  pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
  message,
  trigger: 'blur'
})

// 驾驶证号校验
export const driverLicense = (message = '请输入有效的驾驶证号') => ({
  pattern: /^[1-9]\d{11}$/,
  message,
  trigger: 'blur'
})

// 创建完整的表单规则集
export const createRules = (config) => {
  const rules = {}
  
  for (const key in config) {
    if (Array.isArray(config[key])) {
      rules[key] = config[key]
    } else {
      rules[key] = [config[key]]
    }
  }
  
  return rules
}

// 常用规则集
export const userRules = {
  username: [
    required('请输入用户名'),
    lengthRange(3, 20, '用户名长度应在3到20个字符之间')
  ],
  password: [
    required('请输入密码'),
    lengthRange(6, 20, '密码长度应在6到20个字符之间')
  ],
  name: [
    required('请输入真实姓名'),
    lengthRange(2, 10, '姓名长度应在2到10个字符之间')
  ],
  email: [
    required('请输入邮箱'),
    email()
  ],
  phone: [
    required('请输入手机号'),
    phone()
  ]
}

export const vehicleRules = {
  plateNumber: [
    { required: true, message: '请输入车牌号', trigger: 'blur' },
    { pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/, message: '请输入正确格式的车牌号', trigger: 'blur' }
  ],
  vehicleType: [
    { required: true, message: '请选择车辆类型', trigger: 'change' }
  ],
  brand: [
    { required: true, message: '请输入车辆品牌', trigger: 'blur' }
  ],
  color: [
    { required: true, message: '请输入车辆颜色', trigger: 'blur' }
  ],
  status: [
    { required: true, message: '请选择车辆状态', trigger: 'change' }
  ],
  frameNumber: [
    { required: true, message: '请输入车架号', trigger: 'blur' }
  ],
  engineNumber: [
    { required: true, message: '请输入发动机号', trigger: 'blur' }
  ]
}

export const driverRules = {
  name: [
    { required: true, message: '请输入驾驶员姓名', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  idCard: [
    { required: true, message: '请输入身份证号', trigger: 'blur' },
    { pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: '请输入正确的身份证号码', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  licenseNumber: [
    { required: true, message: '请输入驾驶证号', trigger: 'blur' }
  ],
  licenseType: [
    { required: true, message: '请选择驾驶证类型', trigger: 'change' }
  ],
  licenseExpireDate: [
    { required: true, message: '请选择驾驶证有效期', trigger: 'change' }
  ]
}

export const cardRules = {
  cardNumber: [
    required('请输入卡号'),
    lengthRange(8, 20, '卡号长度应在8到20个字符之间')
  ],
  type: [required('请选择卡类型')],
  status: [required('请选择卡状态')],
  balance: [
    required('请输入当前余额'),
    numberRange(0, 1000000, '余额应在0到1000000之间')
  ]
}

export const rechargeRules = {
  cardNumber: [required('请选择卡号')],
  rechargeDate: [required('请选择充值时间')],
  amount: [
    required('请输入充值金额'),
    numberRange(0, 10000, '充值金额应在0到10000之间')
  ],
  paymentMethod: [required('请选择支付方式')],
  operator: [required('请输入操作人')]
}

export const vehicleUsageRules = {
  plateNumber: [required('请选择车牌号')],
  driver: [required('请选择驾驶员')],
  startTime: [required('请选择起始时间')],
  startMileage: [
    required('请输入起始里程'),
    numberRange(0, 1000000, '里程应在0到1000000之间')
  ],
  fuelLevel: [
    required('请输入油表读数'),
    numberRange(0, 100, '油表读数应在0到100之间')
  ],
  destination: [required('请输入用车地点')],
  purpose: [required('请输入用车目的')]
}

export const fuelUsageRules = {
  plateNumber: [required('请选择车牌号')],
  driver: [required('请选择驾驶员')],
  fuelDate: [required('请选择加油时间')],
  fuelType: [required('请选择油品')],
  volume: [
    required('请输入加油量'),
    numberRange(0, 200, '加油量应在0到200升之间')
  ],
  price: [
    required('请输入单价'),
    numberRange(0, 20, '单价应在0到20之间')
  ],
  amount: [
    required('请输入总金额'),
    numberRange(0, 2000, '总金额应在0到2000之间')
  ],
  currentMileage: [
    required('请输入当前里程'),
    numberRange(0, 1000000, '里程应在0到1000000之间')
  ]
}

export const violationRules = {
  plateNumber: [required('请选择车牌号')],
  driver: [required('请选择驾驶员')],
  violationDate: [required('请选择违章时间')],
  violationType: [required('请选择违章类型')],
  location: [required('请输入违章地点')],
  points: [
    required('请输入扣分'),
    numberRange(0, 12, '扣分应在0到12分之间')
  ],
  fine: [
    required('请输入罚款'),
    numberRange(0, 10000, '罚款应在0到10000元之间')
  ],
  status: [required('请选择状态')]
}

export const accidentRules = {
  plateNumber: [required('请选择车牌号')],
  driver: [required('请选择驾驶员')],
  accidentDate: [required('请选择事故时间')],
  location: [required('请输入事故地点')],
  accidentType: [required('请选择事故类型')],
  description: [required('请输入事故描述')],
  liability: [required('请输入责任认定')],
  damage: [required('请输入车辆损坏情况')],
  repairCost: [
    required('请输入维修费用'),
    numberRange(0, 100000, '维修费用应在0到100000元之间')
  ],
  status: [required('请选择处理状态')]
}

export const insuranceRules = {
  plateNumber: [required('请选择车牌号')],
  driver: [required('请选择驾驶员')],
  insuranceType: [required('请选择保险类型')],
  insuranceAmount: [
    required('请输入保险金额'),
    numberRange(0, 1000000, '保险金额应在0到1000000元之间')
  ],
  company: [required('请输入保险公司')],
  policyNumber: [required('请输入保单号')],
  startDate: [required('请选择起保日期')],
  endDate: [required('请选择终保日期')],
  premium: [
    required('请输入保费'),
    numberRange(0, 50000, '保费应在0到50000元之间')
  ],
  handler: [required('请输入经办人')],
  handlingDate: [required('请选择办理时间')]
}

export const serviceRules = {
  plateNumber: [required('请选择车牌号')],
  driver: [required('请选择驾驶员')],
  serviceDate: [required('请选择保养日期')],
  serviceType: [required('请选择保养类型')],
  serviceItems: [required('请输入保养项目')],
  serviceShop: [required('请输入保养店')],
  serviceCost: [
    required('请输入保养费用'),
    numberRange(0, 10000, '保养费用应在0到10000元之间')
  ],
  currentMileage: [
    required('请输入保养里程'),
    numberRange(0, 1000000, '里程应在0到1000000之间')
  ],
  nextServiceMileage: [
    required('请输入下次保养里程'),
    numberRange(0, 1000000, '里程应在0到1000000之间')
  ],
  nextServiceDate: [required('请选择下次保养时间')],
  status: [required('请选择状态')],
  handler: [required('请输入经办人')],
  handlingDate: [required('请选择办理时间')]
}

export const inspectionRules = {
  plateNumber: [required('请选择车牌号')],
  driver: [required('请选择驾驶员')],
  inspectionType: [required('请选择检测类型')],
  inspectionDate: [required('请选择年检时间')],
  location: [required('请输入年检地点')],
  inspectionItems: [required('请输入检测项目')],
  inspectionCost: [
    required('请输入年检费用'),
    numberRange(0, 2000, '年检费用应在0到2000元之间')
  ],
  result: [required('请选择检测结果')],
  validPeriod: [
    required('请输入有效期'),
    numberRange(0, 24, '有效期应在0到24个月之间')
  ],
  nextInspectionDate: [required('请选择下次年检时间')],
  handler: [required('请输入经办人')],
  handlingDate: [required('请选择办理时间')]
}

export const repairRules = {
  plateNumber: [required('请选择车牌号')],
  driver: [required('请选择驾驶员')],
  repairType: [required('请选择维修类型')],
  startDate: [required('请选择起修时间')],
  endDate: [required('请选择终修时间')],
  location: [required('请输入维修地点')],
  repairItems: [required('请输入维修项目')],
  repairCost: [
    required('请输入维修费用'),
    numberRange(0, 50000, '维修费用应在0到50000元之间')
  ],
  currentMileage: [
    required('请输入当前里程'),
    numberRange(0, 1000000, '里程应在0到1000000之间')
  ],
  handler: [required('请输入经办人')],
  handlingDate: [required('请选择办理时间')],
  status: [required('请选择状态')]
}

export const fuelCardRules = {
  cardNumber: [
    { required: true, message: '请输入加油卡号', trigger: 'blur' }
  ],
  balance: [
    { required: true, message: '请输入余额', trigger: 'blur' },
    { type: 'number', message: '余额必须为数字', trigger: 'blur' }
  ],
  issueDate: [
    { required: true, message: '请选择发卡日期', trigger: 'change' }
  ],
  status: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ]
}

export const etcCardRules = {
  cardNumber: [
    { required: true, message: '请输入ETC卡号', trigger: 'blur' }
  ],
  balance: [
    { required: true, message: '请输入余额', trigger: 'blur' },
    { type: 'number', message: '余额必须为数字', trigger: 'blur' }
  ],
  issueDate: [
    { required: true, message: '请选择发卡日期', trigger: 'change' }
  ],
  expireDate: [
    { required: true, message: '请选择有效期', trigger: 'change' }
  ],
  status: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ]
} 